---
id: running-tracetest-with-opensearch
title: Node.js and OpenSearch
description: Quick start on how to configure a Node.js app with OpenTelemetry traces, OpenSearch as a trace data store, and Tracetest for enhancing your E2E and integration tests with trace-based testing.
hide_table_of_contents: false
keywords:
  - tracetest
  - trace-based testing
  - observability
  - distributed tracing
  - testing
  - opensearch
  - opensearch tracing
  - opensearch observability
  - tracing
  - opentelemetry
image: https://res.cloudinary.com/djwdcmwdz/image/upload/v1698686403/docs/Blog_Thumbnail_14_rsvkmo.jpg
---

:::note
[Check out the source code on GitHub here.](https://github.com/kubeshop/tracetest/tree/main/examples/quick-start-opensearch-nodejs)
:::

[Tracetest](https://tracetest.io/) is a testing tool based on [OpenTelemetry](https://opentelemetry.io/) that allows you to test your distributed application. It allows you to use data from distributed traces generated by OpenTelemetry to validate and assert if your application has the desired behavior defined by your test definitions.

[OpenSearch](https://opensearch.org/) is a scalable, flexible, and extensible open-source software suite for search, analytics, and observability applications licensed under Apache 2.0. OpenSearch started in 2021 as a fork of Elasticsearch and Kibana, with development led by AWS.

## Node.js App with OpenSearch, OpenTelemetry and Tracetest

This is a simple quick start on how to configure a Node.js app to use OpenTelemetry instrumentation with traces, and Tracetest for enhancing your E2E and integration tests with trace-based testing. The infrastructure will use OpenSearch as the trace data store and OpenTelemetry Collector to receive traces from the Node.js app and send them to OpenSearch.

## Prerequisites

**Tracetest Account**:

- Sign up to [`app.tracetest.io`](https://app.tracetest.io) or follow the [get started](/getting-started/overview) docs.
- Have access to the environment's [agent API key](https://app.tracetest.io/retrieve-token).

**Docker**: Have [Docker](https://docs.docker.com/get-docker/) and [Docker Compose](https://docs.docker.com/compose/install/) installed on your machine.

## Run This Quckstart Example

The example below is provided as part of the Tracetest project. You can download and run the example by following these steps:

Clone the Tracetest project and go to the OpenSearch Node.js Quickstart:

```bash
git clone https://github.com/kubeshop/tracetest
cd tracetest/examples/quick-start-opensearch-nodejs
```

Follow these instructions to run the quick start:

1. Copy the `.env.template` file to `.env`.
2. Fill out the [TRACETEST_TOKEN and ENVIRONMENT_ID](https://app.tracetest.io/retrieve-token) details by editing your `.env` file.
3. Run `docker compose run tracetest-run`.
4. Follow the links in the output to view the test results.

Follow along with the sections below for an in detail breakdown of what the example you just ran did and how it works.

## Project Structure

The project contains [Tracetest Agent](/getting-started/install-agent), [OpenSearch](https://github.com/opensearch-project/OpenSearch), [Data Prepper](https://github.com/opensearch-project/data-prepper), [OpenTelemetry Collector](https://github.com/open-telemetry/opentelemetry-collector-contrib), and a Node.js app.

The `docker-compose.yaml` file in the root directory of the quick start runs the Node.js app, OpenSearch, Data Prepper, OpenTelemetry Collector, and the [Tracetest Agent](/concepts/agent).

## Configuring the Node.js App

The Node.js app is a simple Express app, contained in [the `app.js` file](https://github.com/kubeshop/tracetest/blob/main/examples/quick-start-opensearch-nodejs/app.js).

Configure the `.env` like shown below.

```bash
# Get the required information here: https://app.tracetest.io/retrieve-token

TRACETEST_TOKEN="<YOUR_TRACETEST_TOKEN>"
TRACETEST_ENVIRONMENT_ID="<YOUR_ENV_ID>"
```

The OpenTelemetry tracing is contained in the `tracing.otel.grpc.js` or `tracing.otel.http.js` files. Traces will be sent to OpenTelemetry Collector and forwarded from there to Elastic.

Choosing [the `tracing.otel.grpc.js` file](https://github.com/kubeshop/tracetest/blob/main/examples/quick-start-opensearch-nodejs/tracing.otel.grpc.js) will send traces to OpenTelemetry Collector's `GRPC` endpoint.

Enabling the tracer is done by preloading the trace file. As seen in the `package.json`.

```json
"scripts": {
  "app-with-grpc-tracer": "node -r ./tracing.otel.grpc.js app.js",
},
```

How do traces reach OpenSearch?

The [`collector.config.yaml`](https://github.com/kubeshop/tracetest/blob/main/examples/quick-start-opensearch-nodejs/collector.config.yaml) explains that. It receives traces via either `grpc` or `http`. Then it exports them to the Data Prepper that will parse the trace data and send it to OpenSearch. Data Prepper uses the endpoint `data-prepper:21890`.

## Run the Node.js App, OpenSearch, Data Prepper, and OpenTelemetry Collector and Tracetest

The [`docker-compose.yaml` file](https://github.com/kubeshop/tracetest/blob/main/examples/quick-start-opensearch-nodejs/docker-compose.yaml) also contains the Tracetest Agent, OpenTelemetry Collector, OpenSearch, and Data Prepper.

To run everything including Tracetest tests, run this command:

```bash
docker compose run tracetest-run
```

This will:
1. Start the Node.js app, the OpenTelemetry Collector, and send the traces to OpenSearch.
2. Start the Tracetest Agent.
3. Configure the OpenSearch tracing backend and create tests in your environment.
4. Run the tests.

### The Test File

Check out the `resources/test.yaml` file.

```yaml
# resources/test.yaml
type: Test
spec:
  id: W656Q0c4g
  name: Test API
  description: Test the App.
  pollingProfile: jBPzxDCSg
  trigger:
    type: http
    httpRequest:
      url: http://app:8080
      method: GET
      headers:
        - key: Content-Type
          value: application/json
  specs:
    - selector: span[tracetest.span.type="http" name="GET /" http.target="/" http.method="GET"]
      assertions:
        - attr:http.status_code  =  200
        - attr:tracetest.span.duration  <  500ms
```

## Learn More

Feel free to check out our [examples in GitHub](https://github.com/kubeshop/tracetest/tree/main/examples) and join our [Slack Community](https://dub.sh/tracetest-community) for more info!
